package com.jinmei.web.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jinmei.model.WorkScheduleSetting;
import com.jinmei.service.WorkScheduleSettingService;
import com.jinmei.utils.JinmeiConsts;
import com.jinmei.utils.NumberUtil;


public class WorkScheduleSettingAction extends BaseAction {

	private static final long serialVersionUID = -5124993099082336830L;
	private static Logger logger = Logger.getLogger(WorkScheduleSettingAction.class);
	
	private WorkScheduleSettingService workScheduleSettingService;
	private List<WorkScheduleSetting> wsSets = new ArrayList<WorkScheduleSetting>();
	private boolean abselectflag;
	
	public WorkScheduleSettingService getWorkScheduleSettingService() {
		return workScheduleSettingService;
	}

	public void setWorkScheduleSettingService(
			WorkScheduleSettingService workScheduleSettingService) {
		this.workScheduleSettingService = workScheduleSettingService;
	}

	public List<WorkScheduleSetting> getWsSets() {
		if (wsSets != null) {
			for (WorkScheduleSetting setting : wsSets) {
				setting.setAbselectflag(getAbselectflag() ? 1 : 0);
			}
		}
		return wsSets;
	}

	public void setWsSets(List<WorkScheduleSetting> wsSets) {
		this.wsSets = wsSets;
	}

	public boolean getAbselectflag() {
		return abselectflag;
	}

	public void setAbselectflag(boolean abselectflag) {
		this.abselectflag = abselectflag;
	}

	public String getWorkScheduleSetting() throws Exception {
		setUserSessionIdAndUsername();
		
		List<WorkScheduleSetting> settings = getWorkScheduleSettingService().loadAll();
		logger.info("Working schedule setting number: " + settings.size());
		
		setWsSets(settings);
		setAbselectflag(NumberUtil.equels(1, settings.get(0).getAbselectflag()));
		
		return SUCCESS;
	}

	public String saveWorkScheduleSetting() throws Exception {
		setUserSessionIdAndUsername();
		if (getWsSets() != null) {
			getWorkScheduleSettingService().saveOrUpdate(getWsSets());
		}
		
		addActionMessage(JinmeiConsts.MSG_SAVE_SUCCESS);
		return SUCCESS;
      
	}  
	
}
